//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   PartPopupAction.java  $$
//* $$Revision:   1.1  $$
//* $$Author:   JLOPE262  $$
//* $$Date:   Nov 21 2008 14:26:44  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.view.action;

import java.util.Collection;
import java.util.LinkedList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.facade.CommodityFacade;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp01Part;
import ar.com.ford.it.fullEconomics.struct.OptionCombo;
import ar.com.ford.it.fullEconomics.view.form.PartPopupForm;
import ar.com.ford.it.fullEconomics.wslx.FullEconomicsWslxUser;
import ar.com.ford.it.webengine.facade.FacadeProvider;
import ar.com.ford.it.webengine.struts.AbstractBaseAction;
import ar.com.ford.it.webengine.struts.FordActionMapping;
import ar.com.ford.it.webengine.websession.WebSession;

public class PartPopupAction extends AbstractBaseAction {

    private Collection getComboPartClasses(HttpServletRequest request) {
        LinkedList<OptionCombo> list = new LinkedList<OptionCombo>();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);

        list.add(new OptionCombo("-1", resources
                .getMessage("label.selectOption")));
        list.add(new OptionCombo("F", resources.getMessage("label.ford")));
        list.add(new OptionCombo("C", resources.getMessage("label.custom")));

        return list;
    }

    /**
     * 
     */
    public ActionForward initPage(FordActionMapping mapping,
                                  ActionForm actionForm,
                                  HttpServletRequest request,
                                  HttpServletResponse response,
                                  WebSession webSession, ActionMessages msgs,
                                  ActionMessages warns, ActionMessages errorMsgs) {
        PartPopupForm form = (PartPopupForm) actionForm;

        form.resetForm();
        form.setSupplierClassId(request.getParameter("supplierClassId"));
        form.setSupplierCodeId(request.getParameter("supplierCodeId"));
        form.setPartClassId(request.getParameter("partClassId"));
        form.setPartPrefixId(request.getParameter("partPrefixId"));
        form.setPartBaseId(request.getParameter("partBaseId"));
        form.setPartSuffixId(request.getParameter("partSuffixId"));
        request.getSession().setAttribute("partClasses",
                this.getComboPartClasses(request));

        return mapping.findForward("success");
    }

    /**
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward findPart(FordActionMapping mapping,
                                  ActionForm actionForm,
                                  HttpServletRequest request,
                                  HttpServletResponse response,
                                  WebSession webSession, ActionMessages msgs,
                                  ActionMessages warns, ActionMessages errorMsgs) {
        PartPopupForm form = (PartPopupForm) actionForm;
        CommodityFacade commodityFacade = (CommodityFacade) FacadeProvider
                .getFacade(request, CommodityFacade.class);
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();
        Teesp01Part part = null;

        try {
            part = form.getPart();
            part.setEesp01PlantCodeC(user.getTeesg01Plant());
            if ((form.getSupplierClass().equals("-1") || StringUtils
                    .isBlank(form.getSupplierClass()))
                    && StringUtils.isBlank(form.getSupplierCode())) {
                form.setParts(commodityFacade.findParts(part, null, null));
            } else {
                form.setParts(commodityFacade.findParts(part, null, null, form
                        .getSupplierClass(), form.getSupplierCode()));
            }
        } catch (FullEconomicsException e) {
            ActionMessages am = new ActionMessages();
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                    .getMessageResource()));
            saveErrors(request, am);
        }

        return mapping.findForward("success");
    }
}
